package com.blog.service.impl;

import com.blog.dto.PageDto;
import com.blog.entity.Article;
import com.blog.common.result.Result;
import com.blog.entity.User;
import com.blog.mapper.ArticleMapper;
import com.blog.mapper.UserMapper;
import com.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServlet;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.blog.common.constant.ResultCode.METHOD_NOT_ALLOWED;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Result<List<Article>> findArticles(Article article) {
        // 具体实现逻辑
        List<Article> articles = articleMapper.findArticles(article);
        return Result.success(articles);
    }

    /**
     * 根据ID获取文章
     * @return 所有文章
     */
    @Override
    public Result<List<Article>> listAll() {
        List<Article> articleList = articleMapper.listAll();
        return Result.success(articleList);
    }


    @Override
    public Result<Article> getArticleById(Integer id) {
        Article article = articleMapper.getArticleById(id);
        // 具体实现逻辑
        return Result.success(article);
    }



    @Override
    public Result<List<Article>> findArticlesByTag(Integer tagId) {
        List<Article> articles =  articleMapper.findArticlesByTag(tagId);
        // 具体实现逻辑
        return Result.success(articles);
    }

    @Override
    public Result<List<Article>> findArticlesByCategory(Integer categoryId) {
        List<Article> articles = articleMapper.findArticlesByCategory(categoryId);
        return Result.success(articles);
    }

    @Override
    public Result saveArticle( String title,
                               String content,
                               Integer tag,
                               Integer category,
                               String account) {
        if (category == null || tag == null ){  //|| article.getSummary() == null
            return Result.error(METHOD_NOT_ALLOWED);
        }


        String summary=null;

        if (content != null) {

            if (content.length() > 50) {
                summary=content.substring(0, 50);
            } else {
                summary=content;
            }
        }
        Date currentDate = new Date();

        articleMapper.saveArticle(title, content, tag, category,summary,currentDate,account);

        return Result.success();
    }

    @Override
    public Result updateArticle(Article article) {
        //更新时间
        Date currentDate = new Date();
        article.setCreateDate(currentDate);
        articleMapper.updateArticle(article);
        return null;
    }

    @Override
    public Result deleteArticleById(Integer id) {
        articleMapper.deleteArticleById(id);
        return null;
    }
}
